/*
 * DumpingProcessorFactory.java	2008/11/17
 *
 * Copyright 2008 Web & Zen Multimedia, All rights reserved.
 * Use is subject to license terms.
 */
package it.wzm.lightweightlog.annotation;

import it.wzm.lightweightlog.Dumper;
import it.wzm.lightweightlog.DumperException;
import it.wzm.lightweightlog.Entry;
import it.wzm.lightweightlog.Constant.Status;

/**
 * Decorate our Dumper with a set of add-on operations about Severity
 * @author Leonardo Celati
 */
public final class DumpingProcessorFactory extends DumperWrapper {

	/**
	 * As required by serialization
	 */
	private static final long serialVersionUID = 217278902841038708L;
	/**
	 * Holder for filtering
	 */
	private boolean filtering;
	
	/**
	 * The only constructor
	 * @param d a valid Dumper
	 */
	public DumpingProcessorFactory(Dumper d) {		
		super(d);
		// get our annotation and values from it...
		DumpingProcessor sp = d.getClass().getAnnotation(DumpingProcessor.class);
		this.filtering = sp.filtering();
	}
		
	/* 
	 * Wrapping our Dumper
	 * @see it.wzm.lightweightlog.Dumper#write(it.wzm.lightweightlog.Entry)
	 */
	@Override
    public final <E extends Entry> void dump(E e) throws DumperException {
		try {
			//a basic check...
			if (e == null || this.getDumper().getSeverity() ==null)
				throw new DumperException("Entry or Severity are null");
			
	        // just deciding if we need to unload this bean
	        // by simply comparing to dumper severity...
			// obviously if filtering is activated...
			if (filtering && !(e.getSeverity().compareTo(this.getDumper().getSeverity()) > -1)) {			
				return;
			}			
			// dumping...
			this.getDumper().dump(e);
			
		} catch (Exception ex) {
			this.setStatus(Status.FAILURE);
			throw new DumperException("Entry or Severity are null");
		}
    }
	
	@Override
	public final String toString() {
		return "@SeverityPreProcessor" + " - " + this.getDumper().toString() ;
	}

}
